<!doctype html>
<html>

<head>
	<meta charset="UTF-8">
	<title>绑定银行卡</title>
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<link href="./css/mui.min.css" rel="stylesheet" />
	<link href="./css/mui.indexedlist.css" rel="stylesheet" />
	<style>
		html,
		body {
			height: 100%;
			overflow: hidden;
		}

		.mui-bar {
			-webkit-box-shadow: none;
			box-shadow: none;
		}

		.choseBank {
			width: 100%;
			height: 100%;
			overflow: hidden;
			position: absolute;
			top: 0;
			left: 100%;
		}

		.choseBank>.mui-bar {
			position: relative;
		}

		.choseBank>.mui-content {
			padding-top: 0;
		}
	</style>
</head>

<body>
	<header class="mui-bar mui-bar-nav">
		<a class="mui-action-back mui-pull-left"></a>
		<h1 class="mui-title">绑定银行卡</h1>
		<span class="mui-icon mui-icon-close mui-pull-right" onclick="window.history.go(-2);"></span>
	</header>
	<div class="mui-content">
		<div class="mui-card">
			<p style="text-align: center; width: 100%;height: 30px;line-height: 30px;">请绑定本人的银行卡</p>
			<div class="mui-input-group">
				<div class="mui-input-row">
					<label>所属银行</label>
					<!-- <select id="bankList" name="bankName">
						<option selected disabled>选择结算银行</option>
					</select> -->
					<input type="text" id="bankList" readonly class="" placeholder="请选择结算银行">
				</div>
				<input type="hidden" id="payerId" class="mui-input-clear" value="">
				<div class="mui-input-row">
					<label>持卡人名</label>
					<input type="text" id="userName" class="mui-input-clear" placeholder="持卡人姓名" disabled>
				</div>
				<div class="mui-input-row">
					<label>银行卡号</label>
					<input type="text" id="cardNum" class="mui-input-clear" placeholder="请输入卡号">
				</div>
				<div class="mui-input-row">
					<label>预留手机</label>
					<input type="text" id="phone" class="mui-input-clear" placeholder="请输入银行预留手机号">
				</div>
				<div class="mui-input-row">
					<label>绑卡类型</label>
					<select id="cardTypeList" name="cardType">
						<option selected disabled>选择卡类型</option>
						<option value="X">信用卡</option>
						<option value="J">借记卡</option>
					</select>
				</div>
				<div class="mui-input-row expire" style="display: none">
					<label>有效期</label>
					<input id="expire" type="text" class="mui-input-clear" placeholder="格式为月/年,如:0521" data-input-clear="5">
					<span class="mui-icon mui-icon-clear mui-hidden"></span>
				</div>
				<div class="mui-input-row cvv2" style="display: none">

					<label>CVV2</label>
					<input id="cvv2" type="text" class="mui-input-clear" placeholder="卡背后签名栏后三位" data-input-clear="5">
					<span class="mui-icon mui-icon-clear mui-hidden"></span>
				</div>
			</div>
		</div>
		<button type="button" id="confirm-btn" data-loading-text="提交中" class="mui-btn mui-btn-primary confirm-btn" style="width: 90%;height:40px;font-size: 16px;margin-left: 5%;margin-top: 20px;">绑定</button>
	</div>


	<div class="choseBank">
		<div class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-pull-left" onclick="slideRight()"></a>
			<h1 class="mui-title">选择银行</h1>
			<span class="mui-icon mui-icon-close mui-pull-right" onclick="slideRight()"></span>
		</div>
		<div class="mui-content">
			<div id="list" class="mui-indexed-list">
				<div class="mui-indexed-list-search mui-input-row mui-search">
					<input type="search" class="mui-input-clear mui-indexed-list-search-input" placeholder="搜索银行">
				</div>
				<div class="mui-indexed-list-bar">
					<a>A</a>
					<a>B</a>
					<a>C</a>
					<a>D</a>
					<a>E</a>
					<a>F</a>
					<a>G</a>
					<a>H</a>
					<a>I</a>
					<a>J</a>
					<a>K</a>
					<a>L</a>
					<a>M</a>
					<a>N</a>
					<a>O</a>
					<a>P</a>
					<a>Q</a>
					<a>R</a>
					<a>S</a>
					<a>T</a>
					<a>U</a>
					<a>V</a>
					<a>W</a>
					<a>X</a>
					<a>Y</a>
					<a>Z</a>
				</div>
				<div class="mui-indexed-list-alert"></div>
				<div class="mui-indexed-list-inner">
					<div class="mui-indexed-list-empty-alert">没有数据</div>
					<ul class="mui-table-view" id="bankLists">

					</ul>
				</div>
			</div>
		</div>
	</div>


	<script type="text/javascript" src="js/mui.min.js "></script>
	<script type="text/javascript" src="js/jquery.min.js "></script>
	<script type="text/javascript" src="js/jsencrypt.js "></script>
	<script type="text/javascript" src="js/app.js "></script>
	<script type="text/javascript">
        var bankValue = null;
		mui.init({
			swipeBack: true //启用右滑关闭功能
		});
		mui.ready(function () {
			var header = document.querySelector('header.mui-bar');
			var list = document.getElementById('list');
			var payerId = sessionStorage.getItem("payerId");
			//calc hieght
			list.style.height = (document.body.offsetHeight - header.offsetHeight) + 'px';
			$.getJSON("/thmlfApi/avs/pay/getusername?payerId=" + payerId + "&_d=" + Date.parse(new Date()),
				null,
				function (r) {
					if (r.code == 200) {
						$('#userName').val(r.result);
					} else
						mui.alert(r.message);
				});


			mui.getJSON('/thmlfApi/avs/pay/accessbanklistbyindex', null, function (r) {
				if (r.code === '200') {
					var listGroup = r.result,
						htmlStr = '';
					listGroup.forEach(function (item) {
						var dataGroup = Object.keys(item)[0],
							groupKey = dataGroup.toUpperCase(),
							banks = item[dataGroup];

						var groupStr = '<li data-group="' + groupKey +
							'" class="mui-table-view-divider mui-indexed-list-group">' +
							groupKey + '</li>';

						banks.forEach(function (listItem) {
							groupStr += ('<li class="mui-table-view-cell mui-indexed-list-item" data-bank-value="' + listItem.bankCode + "##" + listItem.bankName + '">' +
								listItem.bankName + '</li>');
						});
						htmlStr += groupStr;
					});
					$('#bankLists').append(htmlStr);
                    //create
                    window.indexedList = new mui.IndexedList(list);
				}else {
				    mui.alert(r.message);
                }
			});


			$('#cardTypeList').change(function (e) {
				var value = $(this).val();
				if (value == 'X') {
					$('.expire').css('display',
						'block');
					$('.cvv2').css('display', 'block');
					mui.toast('信用卡不能作为结算卡');
				} else {
					$('.expire').css('display', 'none');
					$('.cvv2').css('display', 'none');
				}
			});

			mui("#list").on('tap', '.mui-indexed-list-item', function () {
				var chosed = this.innerText;
				bankValue = this.getAttribute("data-bank-value");
				console.log('chosed', bankValue);
				$("#bankList").val(chosed);
				slideRight();
			});

			$(".mui-input-group").on('tap', '#bankList', slideRight);

		});
		mui(document.body).on('tap', '.confirm-btn', function () {
			var btn = mui(this);
			// var bankName = $('#bankList').val();
			var cardType = $('#cardTypeList').val();
			var holdName = $('#userName').val();
			var phone = $('#phone').val();
			var cardNum = $('#cardNum').val();
			var payerId = sessionStorage.getItem("payerId");
			var cvv2 = null;
			var expire = null;
			if (bankValue == null) {
				mui.alert("请选择结算银行");
				return false;
			}
			if (cardNum == null) {
				mui.alert("请选择银行卡类型");
				return false;
			}
			if (phone == null) {
				mui.alert("请输入预留手机号");
				return false;
			}
			if (cardNum == null) {
				mui.alert("请输入银行卡号");
				return false;
			} else if (!(/^\d{14,19}$/).test(cardNum)) {
				mui.alert("银行卡号输入有误");
				return false;
			}
			if (cardType == 'X') {
				cvv2 = $('#cvv2').val();
				expire = $('#expire').val();
				if (cvv2 == null || cvv2 == '' || cvv2.length != 3) {
					mui.alert("请输入信用卡CVV2");
					return false;
				}
				if (expire == null || expire == '' || expire.length != 4) {
					mui.alert("请输入信用卡有效期");
					return false;
				}
			} else {
				cvv2 = null;
				expire = null;
			}
			btn.button('loading');
			$.ajax({
				url: "/thmlfApi/avs/pay/adddepositcard" + "?_d=" +
					Date.parse(new Date()),
				type: "POST",
				cache: false,
				data: {
					bankName: bankValue,
					cardType: cardType,
					cardNum: cardNum,
					phone: phone,
					holdName: holdName,
					payerId: payerId,
					cvv2: RSA(cvv2),
					validity: RSA(expire)
				},
				success: function (r) {
					btn.button('reset');
					if (r.code == 200) {
						mui.alert("绑卡成功!");
						window.history.back();
					} else mui.alert(r.message);
				},
				error: function (e) {
					btn.button('reset');
					console.log("request error:" + e);
					mui.alert("网络错误，请稍后再试");
				}
			});
		});

		function slideRight() {
			var wrap = $('.choseBank');
			if (wrap.css('left') === '0px') {
				wrap.animate({
					left: '100%'
				}, "normal")
			} else {
				wrap.animate({
					left: '0'
				}, "normal")
			}
		}
	</script>
	<script type="text/javascript" src="js/mui.indexedlist.js"></script>
</body>

</html>